package aam.controller;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.bind.annotation.ResponseBody;

import aam.model.MessageListVO;
import aam.model.member.MemberVO;
import aam.model.member.MessageVO;
import aam.service.MemberService;

@Controller
public class MemberController {
	@Resource(name="memberServiceImpl")
	private MemberService memberService;
	
	@RequestMapping("/member_login.do")
	public String Login(HttpSession session, MemberVO vo) throws Exception{
		String path="";
		MemberVO mvo = memberService.login(vo);
		if(mvo!=null){
			MemberVO uvo = memberService.updateGrade(mvo);
			session.setAttribute("vo", uvo);
			path="login_result.member";
		}else{
			path="login_fail.member";
		}
		return path;
	}
	@RequestMapping("/member_logout.do")
	public String Logout(HttpSession session){
		session.invalidate();
		return "redirect:board_home.do";
	}
	@RequestMapping("/member_getMemberInfo.do")
	public ModelAndView getMemberInfo(String id){
		return new ModelAndView();
	}
	@RequestMapping("/member_registerMemberInput.do")
	public String registerMemberInput() throws Exception{
		return "register.member";
	}
	@RequestMapping("/member_ajaxIdCheck.do")
	@ResponseBody
	public String ajaxIdCheck(String id)throws Exception{
		String mvo=this.memberService.ajaxIdCheck(id);
		String flag="true";
		if(mvo!=null){
			flag="false";
		}
		return flag;
	}
	@RequestMapping("/member_ajaxNickCheck.do")
	@ResponseBody
	public String ajaxNickCheck(String nick)throws Exception{
		String mvo=this.memberService.ajaxNickCheck(nick);
		String flag="true";
		if(mvo!=null){
			flag="false";
		}
		return flag;
	}
	@RequestMapping("/member_registerMember.do")
	public String registerMember(MemberVO mvo)throws Exception{
		this.memberService.registerMember(mvo);
		return "register_result.member";
	}
	@RequestMapping("/checkAttend.do")
	@ResponseBody
	public String checkAttend(HttpSession session)throws Exception{
		String result="";
		MemberVO vo=(MemberVO) session.getAttribute("vo");
		boolean flag=this.memberService.checkAttend(vo.getId());
		if(flag){
			result="출석완료! 10point가 증가 되었습니다.";
			MemberVO mvo=this.memberService.login(vo);
			session.setAttribute("vo", mvo);
		}else{
			result="오늘 이미 출석하였습니다.";
		}
		return result;
	}

	@RequestMapping("/member_userInfo.do")
	public String userInfo()
	{
		return "WEB-INF/view/member/user_info";
	}
	
	@RequestMapping("/moveUpdatePage.do")
	public String moveUpdatePage()
	{
		return "WEB-INF/view/member/update_info";
	}
	
	@RequestMapping("/updateInfo.do")
	public ModelAndView updateUserInfo(HttpServletRequest request, MemberVO mvo) throws Exception
	{
		memberService.updateUserInfo(mvo);
		MemberVO vo = memberService.login(mvo);
		HttpSession session = request.getSession(false);
		session.setAttribute("vo", vo);
		return new ModelAndView("WEB-INF/view/member/update_ok");
	}
	
	@RequestMapping("/member_getmessage.do")
	public ModelAndView getMessage(HttpSession session,String pageNo) throws Exception{
		MemberVO vo=(MemberVO) session.getAttribute("vo");
		MessageListVO list=this.memberService.getMessage(vo.getId(),pageNo);
		if(list.getMessageList().isEmpty()){
			list=null;
		}
		return new ModelAndView("get.message","message",list);
	}
	@RequestMapping("/member_sendmessage.do")
	public ModelAndView sendMessage(HttpSession session,String pageNo) throws Exception{
		MemberVO vo=(MemberVO) session.getAttribute("vo");
		MessageListVO list=this.memberService.sendMessage(vo.getId(),pageNo);
		if(list.getMessageList().isEmpty()){
			list=null;
		}
		return new ModelAndView("send.message","message",list);
	}
		@RequestMapping("/member_inputMessage.do")
		public String inputMessage(){
			return "inputmessage.message";
		}
		@RequestMapping("/member_ajaxMessageFindByNick.do")
		@ResponseBody
		public HashMap ajaxMessageFindByNick(String nick) throws Exception{
			String id=this.memberService.ajaxNickCheck(nick);
			HashMap map=new HashMap();
			if(id==null){
				map.put("flag", "false");
				return map;
			}
			map.put("flag", "true");
			map.put("id", id);
			return map;
		}
		@RequestMapping("/member_forwardingMessage.do")
		@ResponseBody
		public String forwardingMessage(String id,String content,HttpSession session) throws Exception{
			MemberVO vo=(MemberVO) session.getAttribute("vo");
			this.memberService.forwardingMessage(vo.getId(), content, id);
			return "쪽지를 성공적으로 보내었습니다";
		}
		@RequestMapping("/member_getMessageInfo.do")
		public ModelAndView getMessageInfo(String no) throws Exception{
			MessageVO m=this.memberService.getMessageInfo(Integer.parseInt(no));
			return new ModelAndView("get_messageinfo.message","m",m);
		}
		@RequestMapping("/member_sendMessageInfo.do")
		public ModelAndView sendMessageInfo(String no) throws Exception{
			MessageVO m=this.memberService.sendMessageInfo(Integer.parseInt(no));
			return new ModelAndView("send_messageinfo.message","m",m);
		}
		@RequestMapping("/deleteGetMessage.do")
		public String deleteGetMessage(String no) throws Exception{
			this.memberService.deleteGetMessage(Integer.parseInt(no));
			return "redirect:/member_getmessage.do";
		}
		@RequestMapping("/deleteSendMessage.do")
		public String deleteSendMessage(String no) throws Exception{
			this.memberService.deleteSendMessage(Integer.parseInt(no));
			return "redirect:/member_sendmessage.do";
		}
		
		@RequestMapping("/member_loginfail.do")
		public String loginFail()
		{
			return "login_null.member";
		}
}
